package cn.dida.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>分页支持类</p>
 * @author weiwei 
 */
public class Pageable<T> {
    private int page;
    private int pageSize;
    private int resultTotal;
    private String rquesturl = "";
  

	private List<T> result = new ArrayList<T>();

    public Pageable(int page, int pageSize, int resultTotal, List<T> result) {
        this.page = page;
        this.pageSize = pageSize;
        this.resultTotal = resultTotal;
        this.result = result;
    }

    public Pageable(int page, int resultTotal, List<T> result) {
        this(page, 20, resultTotal, result);
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void refresh() {

    }

    public int getPageTotal() {
        if (resultTotal % pageSize == 0) {
            return resultTotal / pageSize;
        } else {
            return resultTotal / pageSize + 1;
        }
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getRquesturl() {
  		return rquesturl;
  	}

  	public void setRquesturl(HttpServletRequest request,Map<String, Object> paramMap) {
  		
  	     
        String uri = request.getRequestURI();
        String paramKey = "?";
        
        for(String key : paramMap.keySet()){ 
        	if(!key.equals("page")){
        		paramKey += key+"="+paramMap.get(key)+"&";
        	}
		} 
        paramKey = paramKey.substring(0,paramKey.length());
  		
  		this.rquesturl = uri+paramKey;
  	}
    
    public int getResultTotal() {
        return resultTotal;
    }

    public void setResultTotal(int resultTotal) {
        this.resultTotal = resultTotal;
    }

    public List<T> getResult() {
        return result;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }

}
